import os
import time

import logo.Mlog
from dataTemp.step import Step


class Action:
    # 任务名字
    name: str = None
    # 任务时常
    limit_time: int = None
    # 任务完成时间
    complete_time: int = None
    # 任务进度
    __rate: int = 0
    # 任务是否完成
    is_complete: bool = False
    # 步骤列表
    steps: [Step] = []
    # 判断当前的动作是打怪还是点击按钮
    curr_step: Step = None
    step_curr_fail_numb: int = 0

    def __init__(self, name: str):
        pt = name.split("_")
        self.click_or_strange = int(pt[1])
        self.name = pt[0]

    # 下一步
    def next(self) -> Step:
        # logo.Mlog.logger.debug(len(self.steps))
        if len(self.steps) == self.__rate:
            self.is_complete = True
            return None
        s = self.steps[self.__rate]
        self.__rate += 1
        return s

    # 下一步
    def up(self) -> Step:
        self.__rate -= 1
        if self.__rate <= 0:
            self.__rate = 0
        return self.steps[self.__rate]

    # def start(self) -> bool:
    #     if self.click_or_strange:
    #         self.strange()
    #     else:
    #         self.click()
    def click(self,tempTimes: int = 25, totalTimes: int = 50) -> bool:
        self.limit_time = 0
        self.curr_step = self.next()
        while True:
            if self.is_complete:
                logo.Mlog.logger.debug(f"任务完成={self.name}")
                return True
            ok = self.curr_step.check()
            logo.Mlog.logger.debug(f"任务={self.limit_time}")
            if ok:
                self.curr_step = self.next()
            else:
                self.limit_time += 1
            logo.Mlog.logger.debug(f"外围 循环匹配.。。。。。{len(self.steps)}={self.__rate}")
            if len(self.steps)+1 == self.__rate:
                logo.Mlog.logger.debug(f"任务完成2={self.name}")
                self.is_complete = True
                return True
            while not ok and self.limit_time >= 7:
                self.limit_time = 0
                self.__rate += 1
                if len(self.steps) <= self.__rate:
                    self.__rate = 0
                self.curr_step = self.steps[self.__rate]
                if self.curr_step is None:
                    self.__rate = 0
                    continue
                ok = self.curr_step.check(False)
                if ok:
                    if self.name == "goToBuWanJia" and self.curr_step.img_path == '.\\tasks\\actions\\goToBuWanJia_0\\05_0.jpg':
                        self.__rate += 1
                        logo.Mlog.logger.debug(f"当前步骤 +1 ={self.curr_step.img_path}")
                    # logo.Mlog.logger.warning(f"循环匹配到了")
                    break
            time.sleep(1)

    # def click(self, duration: float = 1, tempTimes: int = 25, totalTimes: int = 50) -> bool:
    #     self.limit_time = 0
    #     self.curr_step = self.next()
    #     while True:
    #         self.limit_time += 1
    #         # logo.Mlog.logger.debug(f"当前步骤={self.curr_step.img_path}")
    #         if self.limit_time >= totalTimes:
    #             logo.Mlog.logger.debug(f"当前任务失败次数过多={self.name}")
    #             return False
    #         # logo.Mlog.logger.debug(f"任务。。。")
    #         if self.is_complete:
    #             logo.Mlog.logger.debug(f"任务完成={self.name}")
    #             return True
    #         while not self.curr_step.check():
    #             self.step_curr_fail_numb += 1
    #             logo.Mlog.logger.warning(f"当前步骤={self.step_curr_fail_numb}")
    #             time.sleep(duration)
    #             if self.step_curr_fail_numb > tempTimes:
    #                 self.step_curr_fail_numb = 0
    #                 self.curr_step = self.up()
    #                 # 当前动作失败次数过多
    #                 logo.Mlog.logger.debug(f"当前步骤失败={self.step_curr_fail_numb}")
    #                 continue
    #         else:
    #             self.curr_step = self.next()
    #             # logo.Mlog.logger.debug(f"当前步骤成功进入下一个步骤={self.curr_step.img_path}")
    #         time.sleep(duration)
    # def strange(self):
    #     while True:
    #         step = self.next()
    #         if self.is_complete:
    #             logo.Mlog.logger.debug(f"任务完成={self.name}")
    #             return True
    #         logo.Mlog.logger.debug(f"任务={self.name}")
    #         step.player()
    #         time.sleep(0.5)

    def add_step(self, step_path: str):
        self.steps = []
        # 遍历当前文件夹下的所有项目
        for entry in os.listdir(step_path):
            full_path = os.path.join(step_path, entry)
            # if os.path.isdir(full_path):
            step = Step()
            step.get_image_paths(full_path)
            self.steps.append(step)

    pass
